﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Kugar.UI.Web.AdminLTE.Helpers.FormExtMethod;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_MultiImageUploadField2:PageForm_SingleInputFieldBase
    {
        private SelectorItem[] _lst = null;
        //private string _name = "";
        private bool _disabled = false;
        private string _uploadUrl = "";
        private int _fileSize = 0;
        private int _maxCount = -1;
        private int _colSize = 2;
        private int _width = 120;
        private int _height = 120;

        public PageForm_MultiImageUploadField2(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_multi-image-uploader multi-image-uploader-field");
            this.ControlWidth("");
        }

        //public PageForm_MultiImageUploadField Name(string name)
        //{
        //    _name = name;

        //    return this;
        //}

        public PageForm_MultiImageUploadField2 Images(IEnumerable<string> imageUrls)
        {
            _lst = imageUrls?.Select(x=>new SelectorItem("",x)).ToArrayEx();

            return this;
        }

        public PageForm_MultiImageUploadField2 Images(IEnumerable<SelectorItem> images)
        {
            _lst = images.ToArrayEx();

            return this;
        }

        public PageForm_MultiImageUploadField2 Images<TData>(IEnumerable<TData> lst,Func<TData, string> srcGetter, Func<TData, bool> disableGetter = null)
        {
            _lst = lst.Select(x => new SelectorItem("", srcGetter(x), disableGetter?.Invoke(x) ?? false)).ToArrayEx();

            return this;
        }

        /// <summary>
        /// 每张图片所占用col大小,最大为12,默认为3,即每行4张
        /// </summary>
        /// <param name="col"></param>
        /// <returns></returns>
        public PageForm_MultiImageUploadField2 ColSize(int col)
        {
            _colSize = col;

            return this;
        }

        /// <summary>
        /// 显示图片的大小
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public PageForm_MultiImageUploadField2 Size(int width, int height)
        {
            _width = width;
            _height = height;

            return this;
        }

        /// <summary>
        /// 显示图片的大小
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageForm_MultiImageUploadField2 Size(int size)
        {
            _width = size;
            _height = size;

            return this;
        }

        /// <summary>
        /// 可上传的最大大小,单位为KB
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageForm_MultiImageUploadField2 MaxFileSize(int size)
        {
            _fileSize = size * 1024;
            this.Attributes.Data("maxfilesize", _fileSize.ToStringEx());
            return this;
        }

        public PageForm_MultiImageUploadField2 LimitFileCount(int maxCount)
        {
            _maxCount = maxCount;
            

            return this;
        }


        public PageForm_MultiImageUploadField2 UploadUrl(string url)
        {
            _uploadUrl = url;

            return this;
        }

        public PageForm_MultiImageUploadField2 Disabled(bool disabled=true)
        {
            _disabled = disabled;

            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            this.Data("uploadurl", _uploadUrl)
                .Data("maxfilesize", _fileSize.ToStringEx())
                .Data("disabled", _disabled.ToStringEx())
                .Data("width",_width.ToStringEx())
                .Data("height",_height.ToStringEx())
                .Data("colsize",_colSize.ToStringEx())
                .Data("name", GetFieldName)
                .Data("maxcount", _maxCount.ToStringEx())
                ;

            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {
            writer.WriteLine($"<ul class=\"multi-image-uploader\">");

            var maxCount = _maxCount > 0 ? _maxCount : _lst.Length;

            for (int i = 0; i < maxCount; i++)
            {
                var item = i < (_lst?.Length??0) ? _lst[i] : null;

                writer.WriteLine($"<li class=\"multi-image-uploader-box col-xs-{_colSize}\">");
                
                writer.WriteLine("<div class=\"multi-image-uploader-content\">");

                writer.WriteLine($"<div class=\"row multi-image-uploader-img\"><img width=\"{_width}\" height=\"{_height}\" src=\"{item?.Value}\" /><input type=\"hidden\" name=\"{GetFieldName}\" value=\"{item?.Value}\" /></div>");

                writer.WriteLine("<div class=\"row multi-image-uploader-button\">");

                writer.WriteLine("<div class=\"col-xs-12\">");

                writer.WriteLine("<button class=\"btn btn-sm btn-lazur viewimage\" type=\"button\">查看</button>");

                if (!_disabled)
                {
                    if (_maxCount>0)
                    {
                        writer.WriteLine("<button class=\"btn btn-sm btn-success editimage\" type=\"button\">上传</button>");
                        writer.WriteLine("<button class=\"btn btn-sm btn-warning clearimage\" type=\"button\">清空</button>");
                    }
                    else
                    {
                        writer.WriteLine("<button class=\"btn btn-sm btn-danger deleteimage\" type=\"button\">删除</button>");
                    }
                }

                writer.WriteLine("</div>");

                writer.WriteLine("</div> <!-- .multi-image-uploader-button -->");

                writer.WriteLine("<div class=\"clearfix\"></div>");

                writer.WriteLine("</div> <!-- .multi-image-uploader-content -->");

                writer.WriteLine("<div class=\"clearfix\"></div>");

                writer.WriteLine("</li>");
            }

            if (!_disabled && _maxCount<=0)
            {
                writer.WriteLine(@"<li class=""col-xs-2 addimage"" style=""margin-bottom: 8px;"">
                                    <div style=""width: 100%; padding-top: 2em; padding-bottom: 1em;"">
                                        <div class=""row"" style=""text-align: center"">
                                            <button width=""120"" height=""120"" style=""cursor: pointer; outline: none; border: none"" class=""btn btn-link"" type=""button"">
                                                <i class=""fa fa-fw fa-plus"" style=""font-size: 8em; color: #23c6c8;""></i>
                                            </button>
                                        </div>
                                        <div class=""clearfix""></div>
                                    </div>
                                    <div class=""clearfix""></div>
                                </li>");
            }

            writer.WriteLine("<div class=\"clearfix\"></div>");

            writer.WriteLine("</ul>");
        }
    }
}
